package product;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;

import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JFrame;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.LineBorder;
import javax.swing.JPanel;

import java.awt.Font;
import java.awt.Dimension;
import java.awt.SystemColor;

public class DeliveryRecallUpdate extends JInternalFrame {
	ProductMain4 main;
	String recallNum = "";
	
	JTextField textRecallNum;
	JTextField textCode;
	JTextField textCusNum;
	JTextField textEa;
	JTextField textMemberID;
	private JLabel lblstate;
	private JButton btnUpdate;
	private JButton btnDelete;
	private JButton btnMemBer;
	private JButton btnRecallNum;
	private JPanel panel;
	private JLabel lblReturnNumber;
	private JLabel label_6;
	private JLabel label_7;
	private JLabel label_8;
	private JLabel label_9;
	private JPanel panel_2;
	private JLabel lblRetrunMerchandiseUpdate;
	private JLabel lblRetrunMerchandiseModify;

	/**
	 * Create the frame.
	 */
	public DeliveryRecallUpdate(ProductMain4 main) {
		super("반품수정", false,true,false,true);
		getContentPane().setBackground(SystemColor.text);
		this.main = main;
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 604, 508);
		getContentPane().setLayout(null);
		getContentPane().add(getPanel());
		getContentPane().add(getPanel_2());
		getContentPane().add(getLblRetrunMerchandiseModify());
		getContentPane().add(getLblstate());
	}

	public void update() {
		try {
//			OracleJdbc o = new OracleJdbc();
//			main.conn=o.conn;
			
			String sql = "update delivery_recall set ea=?, member_id=? where num = ?";
			PreparedStatement pstmt = main.conn.prepareStatement(sql);
			pstmt.setString(1, textEa.getText());
			pstmt.setString(2, textMemberID.getText());
			pstmt.setString(3, textRecallNum.getText());

			pstmt.executeUpdate();
			lblstate.setText("수정하였습니다.");
			
			if (main.tList != null) {
				main.tList.searchData();
			}
			
			pstmt.close();
//			main.conn.close();
			
			textCode.setText("");
			textCusNum.setText("");
			textEa.setText("");
			textMemberID.setText("");
			textRecallNum.setText("");
			
			btnUpdate.setEnabled(false);
			btnDelete.setEnabled(false);
		} catch (Exception e) {
		}
	}

	public void delete() {
		try {
//			OracleJdbc o = new OracleJdbc();
//			main.conn=o.conn;
			
			String sql = "delete from delivery_recall where num=?";
			PreparedStatement pstmt = main.conn.prepareStatement(sql);
			pstmt.setString(1, textRecallNum.getText());

			pstmt.executeUpdate();
			lblstate.setText("삭제되었습니다.");
			textCode.setText("");
			textCusNum.setText("");
			textEa.setText("");
			textMemberID.setText("");
			textRecallNum.setText("");
			
			if (main.tList != null) {
				main.tList.searchData();
			}
			
			pstmt.close();
//			main.conn.close();
			
			btnUpdate.setEnabled(false);
			btnDelete.setEnabled(false);
		} catch (Exception e) {
		}
	}

	public void drNumSearch() {
		try {
//			OracleJdbc o = new OracleJdbc();
//			main.conn=o.conn;
			
			lblstate.setText("");
			String sql = "select num,code,customer_num,ea,member_id from delivery_recall where num=?";
			PreparedStatement pstmt = main.conn.prepareStatement(sql);
			pstmt.setString(1, textRecallNum.getText());

			ResultSet rs = pstmt.executeQuery();

			if (rs.next()) {
				textRecallNum.setText(rs.getString(1));
				textCode.setText(rs.getString(2));
				textCusNum.setText(rs.getString(3));
				textEa.setText(rs.getString(4));
				textMemberID.setText(rs.getString(5));
			} else {
				lblstate.setText("자료가 없습니다.");
				textCode.setText("");
				textCusNum.setText("");
				textEa.setText("");
				textMemberID.setText("");
				textRecallNum.setText("");
			}
			rs.close();
			pstmt.close();
//			main.conn.close();
		} catch (Exception ee) {
		}
	}

	public void memberSearch() {
		try {
//			OracleJdbc o = new OracleJdbc();
//			main.conn=o.conn;
			
			boolean flag = true;
			String sql = "select irum from memberdata where memberid=?";
			PreparedStatement pstmt = main.conn.prepareStatement(sql);
			pstmt.setString(1, textMemberID.getText());
			ResultSet rs = pstmt.executeQuery();

			while (rs.next()) {
				String name = rs.getString(1);

				lblstate.setText(name + "님 확인되셨습니다.");
				flag = false;
			}
			if (flag == false) {
				btnUpdate.setEnabled(true);
				btnDelete.setEnabled(true);
			}

			if (flag) {
				lblstate.setText("해당 아이디가 없습니다.");
				btnUpdate.setEnabled(false);
				btnDelete.setEnabled(false);
			}
			rs.close();
			pstmt.close();
//			main.conn.close();

		} catch (Exception ee) {
		}
	}

	private JTextField getTextRecallNum() {
		if (textRecallNum == null) {
			textRecallNum = new JTextField();
			textRecallNum.setBorder(new LineBorder(Color.GRAY));
			textRecallNum.setBounds(177, 12, 200, 30);
			textRecallNum.setColumns(10);
		}
		return textRecallNum;
	}

	private JTextField getTextCode() {
		if (textCode == null) {
			textCode = new JTextField();
			textCode.setBorder(new LineBorder(Color.GRAY));
			textCode.setBounds(177, 52, 200, 30);
			textCode.setEnabled(false);
			textCode.setColumns(10);
		}
		return textCode;
	}

	private JTextField getTextCusNum() {
		if (textCusNum == null) {
			textCusNum = new JTextField();
			textCusNum.setBorder(new LineBorder(Color.GRAY));
			textCusNum.setBounds(177, 93, 200, 30);
			textCusNum.setEnabled(false);
			textCusNum.setColumns(10);
		}
		return textCusNum;
	}

	private JTextField getTextEa() {
		if (textEa == null) {
			textEa = new JTextField();
			textEa.setBorder(new LineBorder(Color.GRAY));
			textEa.setBounds(177, 133, 200, 30);
			textEa.setHorizontalAlignment(SwingConstants.RIGHT);
			textEa.setColumns(10);
		}
		return textEa;
	}

	private JTextField getTextMemberID() {
		if (textMemberID == null) {
			textMemberID = new JTextField();
			textMemberID.setBorder(new LineBorder(Color.GRAY));
			textMemberID.setBounds(177, 175, 200, 30);
			textMemberID.setColumns(10);
		}
		return textMemberID;
	}

	private JLabel getLblstate() {
		if (lblstate == null) {
			lblstate = new JLabel("");
			lblstate.setBorder(new LineBorder(Color.GRAY, 2));
			lblstate.setBounds(24, 434, 545, 26);
			lblstate.setOpaque(true);
			lblstate.setForeground(new Color(255, 0, 0));
			lblstate.setBackground(SystemColor.info);
		}
		return lblstate;
	}

	private JButton getBtnUpdate() {
		if (btnUpdate == null) {
			btnUpdate = new JButton("Modify");
			btnUpdate.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
			btnUpdate.setBorder(new LineBorder(Color.GRAY));
			btnUpdate.setBounds(167, 237, 100, 80);
			btnUpdate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					update();
				}
			});
			btnUpdate.setEnabled(false);
		}
		return btnUpdate;
	}

	private JButton getBtnDelete() {
		if (btnDelete == null) {
			btnDelete = new JButton("Delete");
			btnDelete.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
			btnDelete.setBorder(new LineBorder(Color.GRAY));
			btnDelete.setBounds(277, 237, 100, 80);
			btnDelete.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					delete();
				}
			});
			btnDelete.setEnabled(false);
		}
		return btnDelete;
	}

	private JButton getBtnMemBer() {
		if (btnMemBer == null) {
			btnMemBer = new JButton("Check");
			btnMemBer.setFont(new Font("Microsoft JhengHei UI", Font.PLAIN, 15));
			btnMemBer.setBorder(new LineBorder(Color.GRAY));
			btnMemBer.setBounds(405, 172, 100, 30);
			btnMemBer.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					memberSearch();
				}
			});
		}
		return btnMemBer;
	}

	private JButton getBtnRecallNum() {
		if (btnRecallNum == null) {
			btnRecallNum = new JButton("Check");
			btnRecallNum.setFont(new Font("Microsoft JhengHei UI", Font.PLAIN, 15));
			btnRecallNum.setBorder(new LineBorder(Color.GRAY));
			btnRecallNum.setBounds(405, 10, 100, 30);
			btnRecallNum.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					drNumSearch();
				}
			});
		}
		return btnRecallNum;
	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBorder(new LineBorder(Color.GRAY, 2));
			panel.setBackground(SystemColor.inactiveCaptionBorder);
			panel.setBounds(24, 97, 545, 327);
			panel.setLayout(null);
			panel.add(getLblReturnNumber());
			panel.add(getLabel_6());
			panel.add(getLabel_7());
			panel.add(getLabel_8());
			panel.add(getLabel_9());
			panel.add(getTextMemberID());
			panel.add(getTextCusNum());
			panel.add(getTextCode());
			panel.add(getTextRecallNum());
			panel.add(getBtnRecallNum());
			panel.add(getBtnMemBer());
			panel.add(getTextEa());
			panel.add(getBtnUpdate());
			panel.add(getBtnDelete());
		}
		return panel;
	}
	private JLabel getLblReturnNumber() {
		if (lblReturnNumber == null) {
			lblReturnNumber = new JLabel("Return Number");
			lblReturnNumber.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
			lblReturnNumber.setBounds(33, 10, 170, 30);
		}
		return lblReturnNumber;
	}
	private JLabel getLabel_6() {
		if (label_6 == null) {
			label_6 = new JLabel("Employee Name");
			label_6.setHorizontalAlignment(SwingConstants.LEFT);
			label_6.setFont(new Font("Microsoft JhengHei UI", Font.PLAIN, 15));
			label_6.setBounds(33, 172, 170, 30);
		}
		return label_6;
	}
	private JLabel getLabel_7() {
		if (label_7 == null) {
			label_7 = new JLabel("Product Code");
			label_7.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
			label_7.setBounds(33, 52, 170, 30);
		}
		return label_7;
	}
	private JLabel getLabel_8() {
		if (label_8 == null) {
			label_8 = new JLabel("Registration Number ");
			label_8.setPreferredSize(new Dimension(70, 20));
			label_8.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
			label_8.setBounds(33, 92, 170, 30);
		}
		return label_8;
	}
	private JLabel getLabel_9() {
		if (label_9 == null) {
			label_9 = new JLabel("Quantity");
			label_9.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
			label_9.setBounds(33, 132, 170, 30);
		}
		return label_9;
	}
	private JPanel getPanel_2() {
		if (panel_2 == null) {
			panel_2 = new JPanel();
			panel_2.setLayout(null);
			panel_2.setBorder(new LineBorder(Color.GRAY, 2));
			panel_2.setBounds(-21, -21, 702, 78);
			panel_2.add(getLblRetrunMerchandiseUpdate());
		}
		return panel_2;
	}
	private JLabel getLblRetrunMerchandiseUpdate() {
		if (lblRetrunMerchandiseUpdate == null) {
			lblRetrunMerchandiseUpdate = new JLabel("Retrun Merchandise Update");
			lblRetrunMerchandiseUpdate.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 40));
			lblRetrunMerchandiseUpdate.setBounds(62, 17, 552, 61);
		}
		return lblRetrunMerchandiseUpdate;
	}
	private JLabel getLblRetrunMerchandiseModify() {
		if (lblRetrunMerchandiseModify == null) {
			lblRetrunMerchandiseModify = new JLabel("Retrun Merchandise Information");
			lblRetrunMerchandiseModify.setForeground(Color.DARK_GRAY);
			lblRetrunMerchandiseModify.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
			lblRetrunMerchandiseModify.setBounds(24, 67, 346, 30);
		}
		return lblRetrunMerchandiseModify;
	}
}
